package com.xiaofeng.pochi.controller;

import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
import com.xiaofeng.pochi.common.Page;
import com.xiaofeng.pochi.common.Result;
import com.xiaofeng.pochi.config.Config;
import com.xiaofeng.pochi.pojo.ShopOrder;
import com.xiaofeng.pochi.pojo.ShopOrderHistory;
import com.xiaofeng.pochi.pojo.dto.OrderDto;
import com.xiaofeng.pochi.pojo.vo.OrderMonthVo;
import com.xiaofeng.pochi.pojo.vo.OrderPointVo;
import com.xiaofeng.pochi.pojo.vo.ShopOrderVo;
import com.xiaofeng.pochi.pojo.wx.WxOrderVo;
import com.xiaofeng.pochi.service.ShopOrderService;
import com.xiaofeng.pochi.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/09/18/21:19
 * @Description:
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class ShopOrderController {

    @Autowired
    private ShopOrderService shopOrderService;

    @Autowired
    private WxPayConfig wxPayConfig;


    /**
     * 提交订单
     *
     * @param orderDto
     * @return
     */
    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    public Result<ShopOrder> createOrder(@RequestBody OrderDto orderDto) {
        ShopOrder order = shopOrderService.createOrder(orderDto);
        return new Result<>("订单提交成功",order);
    }


    /**
     * 前台分页查询
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/getMyOrder", method = RequestMethod.POST)
    public Result<Page<ShopOrderVo>> getMyOrder(@RequestBody Page<ShopOrderVo> page) {
        page = shopOrderService.getMyOrder(page);
        return new Result<>(page);
    }


    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result<ShopOrderVo> get(@PathVariable Long id) {
        ShopOrderVo vo = shopOrderService.get(id);
        return new Result<>(vo);
    }


    /**
     * 根据id收货
     * @param id
     * @return
     */
    @RequestMapping(value = "/receiveById/{id}", method = RequestMethod.PUT)
    public Result<?> receiveById(@PathVariable Long id) {
        shopOrderService.receiveById(id);
        return new Result<>("收货成功");
    }

    /**
     * 后台分页查询
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    public Result<Page<ShopOrder>> getByPage(@RequestBody Page<ShopOrder> page) {
        page = shopOrderService.getByPage(page);
        return new Result<>(page);
    }


    /**
     * 改变订单状态
     *
     * @param order
     * @return
     */
    @RequestMapping(value = "/changeOrderStatus", method = RequestMethod.PUT)
    public Result<?> changeOrderStatus(@RequestBody ShopOrder order) {
        shopOrderService.changeOrderStatus(order);
        return new Result<>("操作成功");
    }

    /**
     * 查询订单历史
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getHistory/{id}", method = RequestMethod.GET)
    public Result<List<ShopOrderHistory>> getHistory(@PathVariable Long id) {
        List<ShopOrderHistory> list = shopOrderService.getHistory(id);
        return new Result<>(list);
    }

    /**
     * 查询当月订单
     *
     * @return
     */
    @RequestMapping(value = "/monthOrder", method = RequestMethod.GET)
    public Result<List<OrderMonthVo>> monthOrder() {
        List<OrderMonthVo> list = shopOrderService.monthOrder();
        return new Result<>(list);
    }

    /**
     * 查询每种订单的占比
     *
     * @return
     */
    @RequestMapping(value = "/orderPoint", method = RequestMethod.GET)
    public Result<List<OrderPointVo>> orderPoint() {
        List<OrderPointVo> list = shopOrderService.orderPoint();
        return new Result<>(list);
    }

}
